#!/usr/bin/env python
import itk
import sys
import os
import imageTools.ITKUtils.io as io
import seg_after_filter


def combine(fname1, fname2):
   if fname1 == 'None' :
      return fname2
   img1 = io.readImage(fname1, imgMode = 'UShort' )
   img2 = io.readImage(fname2, imgMode = 'UShort')
   cf = itk.AddImageFilter.IUS3IUS3IUS3.New()
   cf.SetInput1(img1)
   cf.SetInput2(img2)
   io.writeImage(cf.GetOutput())
   return "dummy.mha"

def otsu_seg(fname):
   img = io.readImage(fname)
   tmp = sys.path.splitext(fname)
   fout = tmp[0] + "_seg.mha"
   of = itk.OtsuThresholdImageFilter.IF3IF3.New()
   of.SetInput(img)
   of.SetInsideValue(1)
   of.SetOutsideValue(0)
   io.writeImage(of.GetOutput(), fout)
   return fout


def run(scale):
  f1 = 'None'
  for i in range(1,scale) :
     fname = "m_1s_" + str(2*i) + ".mha"  
     size = 1000
     low = 35 - 5*i
     high = 1000
     print low, high,size, fname
     tmp = os.path.splitext(fname)
     output = tmp[0] + "_seg.mha"

     # Use the threshold image filter
     #seg_after_filter.vas_seg(fname, low, high, size, output) 
     fout  = otsu_seg(fname, scale) 
     f = combine(f1, output )
     f1 = f 
       
def main():
    scale = sys.argv[1]
    run_successive_seg(scale)     
if __name__ == '__main__':
    main()


